package com.daocao.yfb.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import com.daocao.yfb.R;
import com.daocao.yfb.api.Api;
import com.daocao.yfb.api.bean.IdBean;
import com.daocao.yfb.api.model.PointDetailModel;
import com.daocao.yfb.base.BaseActivity;
import com.daocao.yfb.util.Network;
import com.daocao.yfb.util.RxLifeUtil;
import com.daocao.yfb.util.Spans;
import com.daocao.yfb.util.ToastUtil;
import com.daocao.yfb.view.BannerLayout;
import com.daocao.yfb.view.V19FrameLayout;

import org.sufficientlysecure.htmltextview.HtmlHttpImageGetter;
import org.sufficientlysecure.htmltextview.HtmlTextView;

import java.net.URL;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class PointDetailActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_point)
    TextView txtPoint;
    @BindView(R.id.txt_order_money)
    TextView txtOrderMoney;
    @BindView(R.id.txt_user)
    TextView txtUser;
    @BindView(R.id.btn_name)
    RelativeLayout btnName;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.btn_buy)
    TextView btnBuy;
    @BindView(R.id.layout_bottom)
    LinearLayout layoutBottom;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_success_num)
    TextView txtSuccessNum;
    @BindView(R.id.txt_desc)
    HtmlTextView txtDesc;
    @BindView(R.id.img_banner)
    BannerLayout imgBanner;
    @BindView(R.id.txt_tips)
    TextView txtTips;

    private Unbinder unbiner;
    private PointDetailModel.DatasBean orderData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_point_detail);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        String id = getIntent().getStringExtra("id");
        getDetail(id);
        txtTitle.setText("积分详情");

    }

    public void getDetail(String id) {

        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        IdBean bean = new IdBean();
        bean.id = id;
        Api.api_service.getPointDetail(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas);
                });

    }

    private void setData(PointDetailModel.DatasBean item) {
        int textSize30 = getResources().getDimensionPixelSize(R.dimen.x30);
        orderData = item;
        Spanned textOne = Spans.with(this).font("白积分:", textSize30, 0xff333333).
                font(item.score + "分", textSize30, 0xffFF6600).
                build();
        txtPoint.setText(textOne);

        Spanned textTwo = Spans.with(this).font("金额:", textSize30, 0xff333333).
                font("￥" + item.money, textSize30, 0xffFF6600).
                build();
        txtOrderMoney.setText(textTwo);

        txtUser.setText(item.merchant);
        txtName.setText(item.name);
        txtSuccessNum.setText("已成交:" + item.buy_count + "笔");

        txtDesc.setHtml(item.desc, new HtmlHttpImageGetter(txtDesc));
        txtTips.setText(item.note);
//        Glide.with(this).load(item.icon).placeholder(R.color.grey).into(imgBanner);
        if (item.photos == null || item.photos.size() == 0) {
            return;
        }
        imgBanner.setViewUrls(item.photos);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_buy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_buy:
                if (orderData == null) {
                    return;
                }
                Intent intent = new Intent(this, SureOrderActivity.class);
                intent.putExtra("title", orderData.name);
                intent.putExtra("money", orderData.money);
                intent.putExtra("id", orderData.id);
                intent.putExtra("icon", orderData.icon);
                startActivity(intent);
                finish();
                break;
        }
    }
}
